<?php

/**
 * ClassName:
 * @auditor LeiJunXiang
 * @time 2024/10/29
 */

namespace app\controller\invite;

use app\BaseController;
use app\factory\Factory;
use app\factory\model\RequestAgreement;
use app\factory\model\ResponseAgreement;
use app\model\invite\Comment;
use app\model\onlineChat\UserRoom;
use app\modelOpreate\invite\Invite;
use app\modelOpreate\onlineChat\HistoryRecordOpe;
use app\modelOpreate\onlineChat\UserRoomOpe;
use app\modelOpreate\user\ForumUserOpe;
use app\tool\invite\PublishInviteHandler;
use app\tool\inviteList\InviteListHandle;
use app\tool\inviteList\PostListSelect;
use app\tool\inviteList\SendCommentHandler;
use app\tool\user\AttentionUserHandler;

class InviteController extends BaseController
{
    /**
     * 发布帖子
     * @return void
     */
    public function publishInvite()
    {
        $tool = Factory::getInstance()->getToolObj(PublishInviteHandler::TOOL_PUBLISH_INVITE_HANDLER);
        //开始发布（请求对象）
        $tool->startPublish($this->request);
        $errCode = $tool->getErrCode();
        $responseObj = Factory::getInstance()->getModelObj(ResponseAgreement::MODEL_RESPONSE_AGREEMENT);

        if ($errCode == FAIL) {
            $responseObj->setErrCode($errCode);
            $responseObj->setErrMsg($tool->getErrMsg());
        } else {
            $responseObj->setErrCode(SUCCESS);
            $responseObj->setErrMsg("");
        }
    }

    /**
     * 帖子列表
     * @param $userId
     * @return void
     */
    public function inviteList($userId)
    {

        $tool = Factory::getInstance()->getToolObj(InviteListHandle::TOOL_INVITE_LIST_HANDLE);
        //(推荐帖子队列)开始处理(用户id)
        $inviteList = $tool->startHandle($userId);
        $responseObj = Factory::getInstance()->getModelObj(ResponseAgreement::MODEL_RESPONSE_AGREEMENT);
        if (empty($inviteList)) {
            $responseObj->setErrCode($tool->getErrCode());
            $responseObj->setErrMsg($tool->getErrMsg());
        } else {
            $responseObj->setErrCode(SUCCESS);
            $responseObj->setErrMsg("");
            $responseObj->setContent($inviteList);
        }
    }

    /**
     * 帖子详情列表
     * @return void
     */
    public function postDetailList($postObjKey)
    {
        $tool = Factory::getInstance()->getToolObj(PostListSelect::TOOL_POST_LIST_SELECT);
        //(帖子详情)开始查询(帖子对象key)
        $postDetail = $tool->startSelect($postObjKey);
        $responseObj = Factory::getInstance()->getModelObj(ResponseAgreement::MODEL_RESPONSE_AGREEMENT);
        if (empty($postDetail)) {
            $responseObj->setErrCode($tool->getErrCode());
            $responseObj->setErrMsg($tool->getErrMsg());
        } else {
            $responseObj->setErrCode(SUCCESS);
            $responseObj->setErrMsg("");
            $responseObj->setContent($postDetail);
        }
    }

    /**
     * 发布评论(父评论对象key/帖子对象key,评论内容,评论用户id)
     * @return void
     */
    public function sendComment()
    {
        $model = Factory::getInstance()->getModelObj(RequestAgreement::MODEL_REQUEST_AGREEMENT);
        $content = $model->getContent();
        $postObjKey = $content['objKey'];
        $commentContent = $content['commentContent'];
        $commentUserId = $content['commentUserId'];
        $tool = Factory::getInstance()->getToolObj(SendCommentHandler::TOOL_SEND_COMMENT_HANDLER);
        //开始评论(父评论对象key/帖子对象key,评论内容,评论用户id)
        $flag = $tool->startComment($postObjKey, $commentContent, $commentUserId);
        $responseObj = Factory::getInstance()->getModelObj(ResponseAgreement::MODEL_RESPONSE_AGREEMENT);
        if (!$flag) {
            $responseObj->setErrCode($tool->getErrCode());
            $responseObj->setErrMsg($tool->getErrMsg());
        } else {
            $responseObj->setErrCode(SUCCESS);
            $responseObj->setErrMsg("");
        }
    }

    /**
     * 删除评论
     * @return void
     */
    public function delComment($userId, $objKey)
    {
        $obj = Comment::createObj();
        //删除评论(用户id,评论对象key)
        $flag = $obj->delComment($userId, $objKey);
        $responseObj = Factory::getInstance()->getModelObj(ResponseAgreement::MODEL_RESPONSE_AGREEMENT);
        if ($flag) {
            $responseObj->setErrCode(SUCCESS);
            $responseObj->setErrMsg("");
        } else {
            $responseObj->setErrCode(FAIL);
            $responseObj->setErrMsg("删除失败，请重试！");
        }
    }

    /**
     * 点赞评论
     */
    public function likeComment($objKey)
    {
        $obj = Comment::createObj();
        //点赞评论(对象key)
        $flag = $obj->likeComment($objKey);
        $responseObj = Factory::getInstance()->getModelObj(ResponseAgreement::MODEL_RESPONSE_AGREEMENT);
        if ($flag) {
            $responseObj->setErrCode(SUCCESS);
            $responseObj->setErrMsg("");
        } else {
            $responseObj->setErrCode(FAIL);
            $responseObj->setErrMsg("点赞失败，请重试！");
        }
    }

    /**
     * 关注好友
     */
    public function attention($attentionUserId, $userId)
    {
        $tool = Factory::getInstance()->getToolObj(AttentionUserHandler::TOOL_ATTENTION_USER_HANDLER);
        //(true/false)开始处理(用户id,被关注的用户id)
        $flag = $tool->startHandler($userId, $attentionUserId);
        $responseObj = Factory::getInstance()->getModelObj(ResponseAgreement::MODEL_RESPONSE_AGREEMENT);
        if ($flag) {
            $responseObj->setErrCode(SUCCESS);
            $responseObj->setErrMsg("");
        } else {
            $responseObj->setErrCode(FAIL);
            $responseObj->setErrMsg($tool->getErrMsg());
        }
    }


    /**
     * 动态列表
     * @return void
     */
    public function DynamicList($userId)
    {
        $tool = Factory::getInstance()->getToolObj(InviteListHandle::TOOL_INVITE_LIST_HANDLE);
        //获取个人发帖列表(用户id)
        $dynamicList = $tool->getPersonPostList($userId);
        $responseObj = Factory::getInstance()->getModelObj(ResponseAgreement::MODEL_RESPONSE_AGREEMENT);
        if (empty($dynamicList)) {
            $responseObj->setErrCode(FAIL);
            $responseObj->setErrMsg("还未发布任何动态，快来发布吧！");
        } else {
            $responseObj->setErrCode(SUCCESS);
            $responseObj->setErrMsg("");
            $responseObj->setContent($dynamicList);
        }
    }

    public function test()
    {

//        $toolObj = Factory::getInstance()->getToolObj(Invite::TOOL_INVITE_OPE);
//        //(帖子队列)查询已发布的最新帖子(查询个数,已浏览帖子对象key队列)
////        $inviteList = $toolObj->selectNewInvite(5, ['invite_17308820836492', 'invite_17308821111825']);
//        //(帖子队列)查询已发布的最热帖子(查询个数，已浏览帖子对象key队列)
//        //selectHotInvite
//        $inviteList = $toolObj->selectHotInvite(0, ['234']);

//        $userOpe = new ForumUserOpe();
//        $userOpe->userId = 2345;
//        $userOpe->save();

//        $tool = Factory::getInstance()->getModelObj(Comment::MODEL_COMMENT);
//        //查询顶级评论（帖子对象key，页数，条数，展示子评论条数）
//        $comment = $tool->selectComment('invite_369760', 1, 5, 4);
//        $responseObj = Factory::getInstance()->getModelObj(ResponseAgreement::MODEL_RESPONSE_AGREEMENT);
//        if ($comment) {
//            $responseObj->setErrCode(SUCCESS);
//            $responseObj->setErrMsg("");
//            $responseObj->setContent($comment);
//        }
        //增加关注用户id(用户id,关注用户id)
        $tool = Factory::getInstance()->getToolObj(HistoryRecordOpe::TOOL_HISTORY_RECORD_OPE);
//        var_dump("查看数组",json_encode([]));
        //新增用户
//        $arr = [
//            'userId' => 234656,
//            'lastTime'=>time(),
//            'attentionUserIdList'=>json_encode([])
//        ];
//        $tool->insertUser($arr);
//        $flag = $tool->addAttentionUserId(234656, 23423464654);
//        var_dump("增加结果：",$flag);
        //获取最新发送时间和发送人用户id(房间号,用户id)
        $arr = $tool->getNewSendTime(686837316, 12332);
        var_dump("查看数组", $arr);
        $modelObj = Factory::getInstance()->getModelObj(UserRoom::MODEL_USER_ROOM);
        //房间号
        $roomNumber = 12345;
        $modelObj->setRoomNumber($roomNumber);
        //用户id
        $userId = 123;
        $modelObj->setUserId($userId);
        //创建时间
        $createTime = time();
        $modelObj->setCreateTime($createTime);
        $toolObj = Factory::getInstance()->getToolObj(UserRoomOpe::TOOL_USER_ROOM_OPE);
        $toolObj->insertToData($modelObj->getAttribute());

    }


}